//电影管理

var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami')
var fs = require('fs')
//引入上传模块
var multer = require('multer');
//上传的配置
var upload = multer({dest:"./public/upload"});

/* GET users listing. */
router.get('/', async(req, res, next)=>{
  res.render('admin/slide/slideadd')
});



  router.post('/img',upload.single('poster'), async(req, res, next)=> {
        let filmid = req.body.filmid
        let result = rename(req)
        res.json(result)
      })
      //改名函数
      function rename(req) {
        //重命名操作
        let oldPath = req.file.destination+"/"+req.file.filename;
        let newPath = req.file.destination+"/"+req.file.filename+req.file.originalname;
        fs.rename(oldPath,newPath,()=>{
          //console.log("重命名成功")
        })
        return {
          state:'ok',
          imgUrl:"/upload/"+req.file.filename+req.file.originalname
        }
       }


router.post('/', async(req, res)=>{
    let slideName = req.body.slideName
    let routeImg = req.body.img_h
    let state = req.body.state
    if(state == 'on'){
        state = 1
    }else{
        state = 0
    }
    let sqlStr = "insert into slide (slideName,routeImg,state) values(?,?,?)"
    let arr = [slideName,routeImg,state]
    await sqlQuery(sqlStr,arr)
    res.json({
        state:"ok",
        content:"添加轮播图成功"
      })

})


module.exports = router;
